package com.deli.pagingforjava.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.LoadState;
import androidx.recyclerview.widget.RecyclerView;

import com.deli.pagingforjava.R;

import org.jetbrains.annotations.NotNull;
/**
 * @Author: QDL
 * @CreateDate: 2021/4/23 10:20
 */
public class SimpleFooterAdapter extends BaseFooterAdapter<SimpleFooterAdapter.FooterViewHolder> {
    public SimpleFooterAdapter(LayoutInflater layoutInflater) {
        super(layoutInflater);
    }

    @Override
    public void onBindViewHolder(@NotNull FooterViewHolder footerViewHolder, @NotNull LoadState loadState) {
        if (loadState instanceof LoadState.Loading) {
            footerViewHolder.retry.setVisibility(View.GONE);
        } else if (loadState instanceof LoadState.Error) {
            footerViewHolder.retry.setVisibility(View.VISIBLE);
            footerViewHolder.retry.setText("点我重试");
        }

        //重试
        footerViewHolder.retry.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRetry();
            }
        });
    }

    @NotNull
    @Override
    public FooterViewHolder onCreateViewHolder(@NotNull ViewGroup viewGroup, @NotNull LoadState loadState) {
        return new FooterViewHolder(layoutInflater.inflate(R.layout.view_footer, viewGroup, false));
    }

    protected static class FooterViewHolder extends RecyclerView.ViewHolder {
        ProgressBar progressBar;
        TextView retry;

        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            progressBar = itemView.findViewById(R.id.progress);
            retry = itemView.findViewById(R.id.retry);
        }
    }
}
